home *** CD-ROM | disk | FTP | other *** search
- Path: news.sinet.slb.com!usenet
- From: "Vinh D. Nguyen" <vnguyen@sugar-land.anadrill.slb.com>
- Newsgroups: comp.lang.c++
- Subject: Re: pointer to 2 dim array
- Date: Tue, 12 Mar 1996 07:59:27 -0600
- Organization: Schlumberger Anadrill
- Message-ID: <3145833F.740C@sugar-land.anadrill.slb.com>
- References: <313B3317.3CD9@gold.interlog.com> <Dnv24o.F2B@mv.mv.com>
- NNTP-Posting-Host: 163.185.118.40
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Michael Furman wrote:
- >
- > In article <313B3317.3CD9@gold.interlog.com>, markg@gold.interlog.com
- > says...
- > >
- > >How can I create a dynamic 2 dimesional array
- > >and be able to be store and retrieve from it?
- >
- > int (x *) [100] = new int[200][100];
- >
- > >
- > >Thanks.
- > >--
- >
- > --
- > <<< If you received it by E-mail: it is a copy of post to the newsgroup
- > >>>
- > ---------------------------------------------------------------
- > Michael Furman, (603)893-1109
- > Geophysical Survey Systems, Inc. fax:(603)889-3984
- > 13 Klein Drive - P.O. Box 97 engr@gssi.mv.com
- > North Salem, NH 03073-0097 71543.1334@compuserve.com
- > ---------------------------------------------------------------
-
- The above approach requires that one dimension is fixed, in this case all rows
- will have length of 100. A more dynamic approach is illustrated in the
- following C function:
-
- int ** 2DAlloc( int nRows, int nCols )
- {
- int ** ppArray = new int * [ nCols ];
- for( int i = 0; i < nCols; i++ )
- ppArray[ i ] = new int [ nRows ];
-
- return ppAray;
- }
-
- After receiving the pointer from 2DAlloc, you can access the array entries
- using the standard array indexing syntax.
-
- To deallocate the array, use the following function:
-
- void 2DFree( int ** ppArray, int nRows )
- {
- for( int i; i < nRows; i++ )
- delete [] ppArray[ i ];
- delete [] ppArray;
- }
-
- Of course, you can also define a C++ class to encapsulate a dynamic 2D array.
-
- --------------------------------------------------------------------------
- * Vinh Nguyen vnguyen@slb.com *
- * Drilling Information Products - Senior Engineer *
- * Anadrill Schlumberger *
- * 200 Gillingham Ln. (713) 275-7524 (Office) *
- * Sugarland, TX 77478 (713) 275-8098 (FAX) *
- --------------------------------------------------------------------------
-